// Milles.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <time.h>
#include "glut.h"

using std::cout;
using std::cin;

const int WW = 800;
const int WH = 600;

enum color {RED, GREEN, BLUE, YELLOW, WILD};
enum value {ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN,
			   EIGHT, NINE, SKIP, DRAW2, REV, WD0, WD4};

struct card {
	int id;
	color col;
	value val;
	card* next;
};

struct player {
	int id;
	int count;
	card* first;
};


float p2x(int x) { float ww = WW; return    ( ( x / ww ) * 2 ) - 1;         }
float p2y(int y) { float wh = WH; return  ( ( ( y / wh ) * 2 ) - 1 ) * -1;  }

void drawSquare(int x, int y, int w, int h)
{
cout << x << " " << y << " " << w << " " << h << " " << p2x(x) << " " << p2y(y) << "\n";
	glBegin(GL_LINE_LOOP);
		glVertex2f(p2x(x),   p2y(y));
		glVertex2f(p2x(x+w), p2y(y));
		glVertex2f(p2x(x+w), p2y(y+h));
		glVertex2f(p2x(x),   p2y(y+h));
	glEnd();
	glFlush();
}

void displayCB( void )
{
	glClearColor (1.0, 1.0, 1.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);
	
	drawSquare(56, 360, 120, 186);
	drawSquare(250, 208, 120, 186);
	drawSquare(405, 208, 120, 186);
	
	//glutPostRedisplay();
}

void keyboardCB(unsigned char key, int x, int y)
{	if (key == 27 || key == 'Q' || key == 'q')   exit(0);  }
	
int main(int argc, char* argv[])
{
	//construct the deck
	card deck[108];
	for (int idx = 0; idx < 108; ++idx) deck[idx].id = idx; // set id field

	for (int idx =   0; idx <  25; ++idx)   deck[idx].col = RED;    
   for (int idx =  25; idx <  50; ++idx)   deck[idx].col = GREEN;   
	for (int idx =  50; idx <  75; ++idx)   deck[idx].col = BLUE;  
	for (int idx =  75; idx < 100; ++idx)   deck[idx].col = YELLOW; 
	for (int idx = 100; idx < 108; ++idx)   deck[idx].col = WILD; 
	
	for (int idx =  0; idx < 100; idx += 25) deck[idx].val = ZERO;
	for (int idx =  1; idx < 100; idx += 25) deck[idx].val = deck[idx+1].val = ONE;
	for (int idx =  3; idx < 100; idx += 25) deck[idx].val = deck[idx+1].val = TWO;
	for (int idx =  5; idx < 100; idx += 25) deck[idx].val = deck[idx+1].val = THREE;
	for (int idx =  7; idx < 100; idx += 25) deck[idx].val = deck[idx+1].val = FOUR;
	for (int idx =  9; idx < 100; idx += 25) deck[idx].val = deck[idx+1].val = FIVE;
	for (int idx = 11; idx < 100; idx += 25) deck[idx].val = deck[idx+1].val = SIX;
	for (int idx = 13; idx < 100; idx += 25) deck[idx].val = deck[idx+1].val = SEVEN;
	for (int idx = 15; idx < 100; idx += 25) deck[idx].val = deck[idx+1].val = EIGHT;
	for (int idx = 17; idx < 100; idx += 25) deck[idx].val = deck[idx+1].val = NINE;
	for (int idx = 19; idx < 100; idx += 25) deck[idx].val = deck[idx+1].val = SKIP;
	for (int idx = 21; idx < 100; idx += 25) deck[idx].val = deck[idx+1].val = DRAW2;
	for (int idx = 23; idx < 100; idx += 25) deck[idx].val = deck[idx+1].val = REV;
	
	for (int idx = 100; idx < 104; ++idx)  deck[idx].val = WD0;
	for (int idx = 104; idx < 108; ++idx)  deck[idx].val = WD4;
	
	//shuffle the bitch
		card* temp; // used as a flag field for shuffling the built deck
	temp = new card;
	
	for (int idx = 0; idx < 108; ++idx)  deck[idx].next = temp; // init next fields
	
	int shuffled = 1;
	card* head;
	
	srand ( time(NULL) );
	int nextRand = rand() % 108;
	deck[nextRand].next = NULL; // set the bottom card of shuffled pile
	head = &deck[nextRand];
	
	while (shuffled < 107) { // there are >1 cards unshuffled
		nextRand = rand() % 108;
		if (deck[nextRand].next == temp) { //the random card hasn't been used yet
			deck[nextRand].next = head;
			head = &deck[nextRand];
			++shuffled;
		}
	}

	int last = 0;	// find the last unshuffled card & make it head of Linked List
	while (deck[last].next != temp)  ++last;
	deck[last].next = head;     head = &deck[last];

	delete[] temp; // turn loose the memory for the temp object

// --------- make four players and deal the cards --------------------------//
	player P[4];

	for (int idx = 0; idx < 4; ++idx) {
		P[idx].id = idx;
		P[idx].count = 7;
		P[idx].first = head;
		head = head->next;
		P[idx].first->next = NULL;
	}

	for (int idx = 0; idx < 24; ++idx) {
		temp = head;
		head = head->next;
		temp->next = P[idx % 4].first;
		P[idx % 4].first = temp;
	}

//GLUT STUFF------------------------------------------------------------------
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WW, WH);
glutInitWindowPosition(100, 100);
glutCreateWindow("Uno, bitch!");
glutDisplayFunc(displayCB);

glClearColor(0.1, 0.1, 0.1, 1.0);
glColor3f(0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glutKeyboardFunc(keyboardCB);

glutMainLoop();


	return 0;
}
